﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using InkFx.WinControl.Core;

namespace Temp_20190707_AForm
{
    public class DockPopPanel : Panel
    {
        private DockPopPanel(DockPanel dockPanel, DockTabGroup dockGroup)
        {
            SetStyle(Win32API.ControlStylesUAORSD, true);
            this.TagForm = dockPanel.FindForm();
            this.DockPanel = dockPanel;
            this.DockTabGroup = dockGroup;
            this.Alignment = dockGroup.Alignment;
            this.ParentControl = dockGroup.Parent;

            if (TagForm != null)
            {
                TagForm.LocationChanged += ResetRect;
                TagForm.VisibleChanged += ResetRect;
            }
            if (DockPanel != null)
            {
                DockPanel.LocationChanged += ResetRect;
                DockPanel.SizeChanged += ResetRect;
            }


            var align = this.Alignment;
            var padding = new Padding(0, 0, 0, 0);
            if (align == DockAlignment.Left) padding = new Padding(0, 0, SPLIT_SIZE, 0);
            else if (align == DockAlignment.Right) padding = new Padding(SPLIT_SIZE, 0, 0, 0);
            else if (align == DockAlignment.Top) padding = new Padding(0, 0, 0, SPLIT_SIZE);
            else if (align == DockAlignment.Bottom) padding = new Padding(0, SPLIT_SIZE, 0, 0);
            base.Padding = padding;
        }



        public static DockPopPanel ActivePopPanle = null;
        public static void TogglePopGroup(DockTabGroup dockGroup)
        {
            bool same = false;
            if (ActivePopPanle != null)
            {
                same = ActivePopPanle.DockTabGroup == dockGroup;
                ActivePopPanle.DockTabGroup.Parent = ActivePopPanle.ParentControl;
                ActivePopPanle.Dispose();
                ActivePopPanle = null;
                if (same) return;
            }

            var dockPanel = dockGroup == null ? null : dockGroup.DockPanel;
            if (dockPanel == null) return;
            DockPopPanel panel = new DockPopPanel(dockPanel, dockGroup);
            panel.Parent = dockPanel;
            panel.BringToFront();
            panel.ResetRect(panel, EventArgs.Empty);
            ActivePopPanle = panel;

            dockGroup.Parent = panel;
        }



        private const int SPLIT_SIZE = 3;
        public Form TagForm { get; private set; }
        public DockPanel DockPanel { get; private set; }
        public DockTabGroup DockTabGroup { get; private set; }
        public DockAlignment Alignment { get; private set; }
        public Control ParentControl { get; private set; }

        public Rectangle RectSplitter { get; private set; }
        public Rectangle RectMovingSplitter { get; private set; }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawSplitter();
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            
            
            var align = this.Alignment;
            
            //if (IsSplitMoving)
            //    SetCursor((align == DockAlignment.Top || align == DockAlignment.Bottom) ? Cursors.HSplit : Cursors.VSplit);
            //else
            //    SetCursor(RectSplitter.Contains(e.Location) ? ((align == DockAlignment.Top || align == DockAlignment.Bottom) ? Cursors.HSplit : Cursors.VSplit) : Cursors.Default);
            //DoMove(e.Location);
            //m_MoveStartPoint = e.Location;
            //DrawSplitHelper();
            //this.Invalidate();
            //return;


            if (DockPanel.IsSplitMoving)
            {
                SetCursor((align == DockAlignment.Top || align == DockAlignment.Bottom) ? Cursors.HSplit : Cursors.VSplit);

                Rectangle rect = RectSplitter;
                Point point = e.Location;
                Point diff = new Point(m_MoveStartPoint.X - point.X, m_MoveStartPoint.Y - point.Y);
                if (align == DockAlignment.Top) this.RectMovingSplitter = new Rectangle(rect.X, rect.Y - diff.Y, rect.Width, rect.Height);
                else if (align == DockAlignment.Bottom) this.RectMovingSplitter = new Rectangle(rect.X, rect.Y - diff.Y, rect.Width, rect.Height);
                else if (align == DockAlignment.Left) this.RectMovingSplitter = new Rectangle(rect.X - diff.X, rect.Y, rect.Width, rect.Height);
                else if (align == DockAlignment.Right) this.RectMovingSplitter = new Rectangle(rect.X - diff.X, rect.Y, rect.Width, rect.Height);
                DrawSplitter();
            }
            else
                SetCursor(RectSplitter.Contains(e.Location) ? ((align == DockAlignment.Top || align == DockAlignment.Bottom) ? Cursors.HSplit : Cursors.VSplit) : Cursors.Default);

            this.Invalidate();
        }

        private Point m_MoveStartPoint = Point.Empty;
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            DockPanel.IsSplitMoving = RectSplitter.Contains(e.Location);
            m_MoveStartPoint = e.Location;
            this.Invalidate();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            DoMove(e.Location);
            DockPanel.IsSplitMoving = false;
            this.Invalidate();
            DockPanel.Invalidate();
        }


        private void SetCursor(System.Windows.Forms.Cursor cursor)
        {
            System.Windows.Forms.Cursor baseCursor = base.Cursor;
            if (baseCursor != null && baseCursor != cursor)
                base.Cursor = cursor;
        }
        private Rectangle GetRectSplitter()
        {
            return DockPanel.IsSplitMoving ? RectMovingSplitter : RectSplitter;
        }

        private void DrawSplitter()
        {
            if (!DockPanel.IsSplitMoving) return;
            Rectangle rectangle = GetRectSplitter();  //RectSplitter; //
            //rectangle.Location = new Point(rectangle.X - this.Location.X, rectangle.Y - this.Location.Y);
            Rectangle rectangle2 = DockPanel.RectangleToClient(this.RectangleToScreen(rectangle));
            //Win32API.DrawSplitHelper(this.Handle, new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height));
            //Win32API.DrawSplitHelper(DockPanel.Handle, rectangle2);
            //DockPanel.Refresh();
            DockPanel.DrawSplitter(rectangle2);

            //Win32API.DrawSplitHelper(this.Handle, rectangle);

        }

        private void DoMove(Point point)
        {
            if (DockPanel.IsSplitMoving)
            {
                Point diff = new Point(m_MoveStartPoint.X - point.X, m_MoveStartPoint.Y - point.Y);

                var align = this.Alignment;
                if (align == DockAlignment.Top) this.Height = this.Height - diff.Y;
                else if (align == DockAlignment.Bottom) this.Height = this.Height + diff.Y;
                else if (align == DockAlignment.Left) this.Width = this.Width - diff.X;
                else if (align == DockAlignment.Right) this.Width = this.Width + diff.X;

                ResetRect(this, EventArgs.Empty);
            }
        }
        private void ResetRect(object sender, EventArgs e)
        {
            //var rect = DockPanel.RectangleToScreen(new Rectangle(Point.Empty, DockPanel.Size));
            //rect.Location = new Point(0, 0);
            var align = this.Alignment;
            var rect = DockPanel.RectDockInner;
            var dockWidth = rect.Width;
            var dockHeight = rect.Height;


            int width = this.Width;
            int height = this.Height;
            if (align == DockAlignment.Left)
            {
                this.Location = new Point(rect.X + 0, rect.Y + 0);
                this.Size = new Size(width, dockHeight);
                this.RectSplitter = new Rectangle(this.Width - SPLIT_SIZE, 0, SPLIT_SIZE, this.Height);
            }
            else if (align == DockAlignment.Right)
            {
                this.Location = new Point(rect.X + (dockWidth - width), rect.Y + 0);
                this.Size = new Size(width, dockHeight);
                this.RectSplitter = new Rectangle(0, 0, SPLIT_SIZE, this.Height);
            }
            else if (align == DockAlignment.Top)
            {
                this.Location = new Point(rect.X + 0, rect.Y + 0);
                this.Size = new Size(dockWidth, height);
                this.RectSplitter = new Rectangle(0, this.Height - SPLIT_SIZE, this.Width, SPLIT_SIZE);
            }
            else if (align == DockAlignment.Bottom)
            {
                this.Location = new Point(rect.X + 0, rect.Y + (dockHeight - height));
                this.Size = new Size(dockWidth, height);
                this.RectSplitter = new Rectangle(0, 0, this.Width, SPLIT_SIZE);
            }
        }


    }
}
